<template>
  <view
    class="bill_box"
    @touchmove="refresh.moveTouchendHandler"
    @touchstart="refresh.startTouchendHandler"
    @touchend="refresh.endTouchendHandler"
  >
    <!-- 菜单列表 -->
    <menu-navigation-bar
      :is-sticky="true"
      :menu-list="menuList"
      @on-selected="onMenuSelect"
    />

    <!-- 内容列表 -->
    <view class="content" v-if="menuIndex == 0">
      <house-bill
        v-for="(item, index) in dataList"
        :key="index"
        :data="item"
        @on-click="onClick(item.billId, item.billCategory)"
      />
    </view>
    <view class="content" v-else-if="menuIndex == 1">
      <living-bill
        v-for="(item, index) in dataList"
        :key="index"
        :data="item"
        @on-click="onClick(item.billId, item.billCategory)"
      />
    </view>
    <view class="content" v-else>
      <others-bill
        v-for="(item, index) in dataList"
        :key="index"
        :data="item"
        @on-click="onClick(item.billId, item.billCategory)"
      />
    </view>

    <!-- 加载状态 -->
    <load :status="loadStatue" :isImage="dataList.length<1" />

    <!-- 下拉刷新 -->
    <pull-down-refresh
      :opacity="refresh.moveBottom.value / 3"
      :enble-anim="refresh.isRefresh.value"
    />
  </view>
</template>

<!-- 账单 -->
<script lang="ts" setup>
import { refreshHandler } from '@/components/common/pull_down_refresh/index'
import livingBill from '@/components/business/bill/living-bill.vue'
import othersBill from '@/components/business/bill/others-bill.vue'
import houseBill from '@/components/business/bill/house-bill.vue'
import menuNavigationBar from '@/components/layout/menu_navigation_bar/menu-navigation-bar.vue'
import type { PageBill, QueryPageBill } from '@/config/data/data'
import { BillTypeEnum } from '@/config/enum/enum'
import { onLoad, onReachBottom } from '@dcloudio/uni-app'
import { ref, watch } from 'vue'
import { ReqSelectPageBillByCategory } from '@/api/bill'

//菜单导航栏
const menuList = ref<string[]>([])

//菜单选中下标
const menuIndex = ref(0)

//下拉刷新控制函数
const refresh = refreshHandler(300)

//加载状态
const loadStatue = ref(1)

//账单数据列表
const dataList = ref<PageBill[]>([])

/**
 * 菜单选中
 * @param index: 选中分类下标
 */
const onMenuSelect = (index: number) => {
  menuIndex.value = index
  queryPage.value.page = 0
  queryPage.value.category = menuList.value[index]
  getData(true)
}

//防止重复请求
const isReq = ref(true)

/**
 * 点击事件
 * @param id: 账单id
 * @param category: 账单类型
 */
const onClick = (id: number, category: string) => {
  uni.navigateTo({
    url: `../../subpkg/bill/detail?id=${id}&category=${category}`,
  })
}

//监听下拉刷新状态
watch(
  () => refresh.isRefresh.value,
  (newValue, oldValue) => {
    if (newValue) {
      //开启刷新，并请求
      queryPage.value.page = 0
      getData(true).finally(() => {
        refresh.refreshEnd(500)
      })
    }
  }
)

//分页请求参数
const queryPage = ref<QueryPageBill>({
  page: 0,
  size: 10,
  category: '',
})

/**
 * 下一页
 */
const getData = (refresh: boolean = false) => {
  return new Promise((reslove) => {
    if (!isReq.value) {
      reslove(true)
      return
    }
    queryPage.value.page += 1
    loadStatue.value = 0
    ReqSelectPageBillByCategory(queryPage.value)
      .then((res) => {
        if (res.code == 200 && res.data) {
          refresh ? (dataList.value = []) : ''
          dataList.value.push(...res.data)
          let bool = res.data.length == queryPage.value.size
          loadStatue.value = bool ? 1 : -1
        }
      })
      .catch(() => {
        loadStatue.value = -2
        setTimeout(() => {
          loadStatue.value = -1
        }, 100)
      })
      .finally(() => {
        isReq.value = true
        reslove(true)
      })
  })
}

//初始化
const initData = () => {
  //菜单列表初始化
  for (let msg in BillTypeEnum) {
    if (BillTypeEnum.hasOwnProperty(msg)) {
      menuList.value.push(BillTypeEnum[msg as keyof typeof BillTypeEnum])
    }
  }
  queryPage.value.category = menuList.value[0]

  getData()
}

//页面加载
onLoad(() => {
  initData()
})

//页面滑动到底部
onReachBottom(() => {
  getData()
})
</script>

<style scoped lang="scss">
.bill_box {
  width: 100%;
  min-height: 100vh;
  background-color: $uni-bg-color-grey;

  .content {
    padding: 0 30rpx;
    width: calc(100% - 60rpx);
  }
}
</style>
